var config=require('config-lite');
var Mongolass=require('mongolass');
var mongolass=new Mongolass();

var moment =require('moment');
var objectIdToTimestamp=require('objectid-to-timestamp');

//根据id生成创建时间
mongolass.plugin('addCreatedAt',{
    afterFind:function(results){
        results.forEach(function(item){
            item.created_at=moment(objectIdToTimestamp(item._id)).format('YYYY-MM-DD HH:mm');

        })
        return results;
    },
    afterFindOne:function(result){
        if(result){
            result.created_at=moment(objectIdToTimestamp(result._id)).format('YYYY-MM-DD HH:MM');
        };
        return result
    }

})



exports.User=mongolass.model('User',{
    name:{type:'string'},
    password:{type:'string'},
    avatar:{type:'string'},
    gengder:{type:'string',enum:['m','n','x']},
    bio:{type:'string'}
    
});


exports.User.index({name:1},{unique:true}).exec();//根据用户名查找用户， 用户名全局唯一

exports.Post=mongolass.model('Post',{
    author:{type:Mongolass.Types.ObjectId},
    title:{type:'string'},
    content:{type:'string'},
    pv:{type:'number'}
})

exports.Post.index({author:1,_id:-1}).exec();//按照创建事件降序查看用户的文章列表


exports.Comment=mongolass.model('Comment',{
    author:{type:Mongolass.Types.ObjectId},
    content:{type:'string'},
    postId:{type:Mongolass.Types.ObjectId}
});

exports.Comment.index({postId:1,_id:1}).exec();//通过文章id获取该文章下所有留言，按照留言创建时间升序
exports.Comment.index({author:1,_id:1}).exec();//通过用户id和留言id删除一个留言

